<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>简介</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.types.html">« 类型</a></li>
      <li style="float: right;"><a href="language.types.type-system.html">类型系统 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.types.html">类型</a></li>
    <li>简介</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.types.intro" class="sect1">
  <h2 class="title">简介</h2>

  <p class="para">
   Every single expression in PHP has one of the following
   built-in types depending on its value:
   <ul class="itemizedlist">
    <li class="listitem"><span class="simpara"><span class="type">null</span></span></li>
    <li class="listitem"><span class="simpara"><span class="type">bool</span></span></li>
    <li class="listitem"><span class="simpara"><span class="type">int</span></span></li>
    <li class="listitem"><span class="simpara"><span class="type">float</span> （浮点数）</span></li>
    <li class="listitem"><span class="simpara"><span class="type">string</span></span></li>
    <li class="listitem"><span class="simpara"><span class="type">array</span></span></li>
    <li class="listitem"><span class="simpara"><span class="type">object</span></span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span></span></li>
    <li class="listitem"><span class="simpara"><span class="type">resource</span></span></li>
   </ul>
  </p>

  <p class="para">
   PHP is a dynamically typed language, which means that by default there is
   no need to specify the type of a variable, as this will be determined at
   runtime. However, it is possible to statically type some aspect of the
   language via the use of
   <a href="language.types.declarations.html" class="link">type declarations</a>.
  </p>

  <p class="para">
   Types restrict the kind of operations that can be performed on them.
   However, if an expression/variable is used in an operation which
   its type does not support, PHP will attempt to
   <a href="language.types.type-juggling.html" class="link">type juggle</a>
   the value into a type that supports the operation.
   This process depends on the context in which the value is used.
   For more information, see the section on
   <a href="language.types.type-juggling.html" class="link">Type Juggling</a>.
  </p>

  <div class="tip"><strong class="tip">小技巧</strong>
   <p class="simpara">
    <a href="types.comparisons.html" class="link">The type comparison tables</a>
    may also be useful, as various examples of comparison between values of
    different types are present.
   </p>
  </div>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    It is possible to force an expression to be evaluated to a certain type by
    using a <a href="language.types.type-juggling.html#language.types.typecasting" class="link">type cast</a>.
    A variable can also be type cast in-place by using the
    <span class="function"><a href="function.settype.html" class="function">settype()</a></span> function on it.
   </span>
  </p></blockquote>

  <p class="para">
   To check the value and type of an
   <a href="language.expressions.html" class="link">expression</a>,
   use the <span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span> function.
   To retrieve the type of an
   <a href="language.expressions.html" class="link">expression</a>,
   use the <span class="function"><a href="function.get-debug-type.html" class="function">get_debug_type()</a></span> function.
   However, to check if an expression is of a certain type use the
   
   <code class="literal">is_<span class="replaceable">type</span></code> functions instead.

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
$a_bool&nbsp;=&nbsp;true;&nbsp;&nbsp;&nbsp;//&nbsp;a&nbsp;bool<br />$a_str&nbsp;&nbsp;=&nbsp;"foo";&nbsp;&nbsp;//&nbsp;a&nbsp;string<br />$a_str2&nbsp;=&nbsp;'foo';&nbsp;&nbsp;//&nbsp;a&nbsp;string<br />$an_int&nbsp;=&nbsp;12;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;an&nbsp;int<br />echo&nbsp;get_debug_type($a_bool),&nbsp;"\n";<br />echo&nbsp;get_debug_type($a_str),&nbsp;"\n";<br /><br />//&nbsp;如果是整型，就加上&nbsp;4<br />if&nbsp;(is_int($an_int))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;$an_int&nbsp;+=&nbsp;4;<br />}<br />var_dump($an_int);<br /><br />//&nbsp;如果&nbsp;$a_bool&nbsp;是字符串，就打印出来<br />if&nbsp;(is_string($a_bool))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"String:&nbsp;$a_bool";<br />}<br />?&gt;</span>
</code></div>
    </div>

    <p class="para">以上例程在 PHP 8 中的输出：</p>
    <div class="example-contents screen">
     <div class="cdata"><pre>
bool
string
int(16)
</pre></div>
    </div>
   </div>
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    Prior to PHP 8.0.0, where the <span class="function"><a href="function.get-debug-type.html" class="function">get_debug_type()</a></span> is not
    available, the <span class="function"><a href="function.gettype.html" class="function">gettype()</a></span> function can be used instead.
    However, it doesn&#039;t use the canonical type names.
   </span>
  </p></blockquote>
 </div></div></div></body></html>